import { reqgetgoodsinfo,reqcartadd } from "../../http/api";
import { Toast } from "antd-mobile";
//state
const initState = {
    info:[]
}
    
//action type
const TYPES = {
    DETAIL_CHANGE_GOODSINFO:"DETAIL_CHANGE_GOODSINFO"
}

//reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.DETAIL_CHANGE_GOODSINFO:
            return{
                ...state,
                info:action.info
            }
        default:
            return state;
    }
}

//action creator
export const actions = {
    changeGoodsinfo:(info) => ({type:TYPES.DETAIL_CHANGE_GOODSINFO,info}),
    getList:(id) => (dispatch,getState) => {
        reqgetgoodsinfo({id}).then(res=>{
            dispatch(actions.changeGoodsinfo(res.data.list[0]))
        })
    },
    toCar:(goodsid) => () => {
        let uid = JSON.parse(sessionStorage.getItem('userInfo')).uid;
        reqcartadd({uid,num:1,type:1,goodsid}).then(res => {
            Toast.show({
                content:res.data.msg
            })
        })
    },

}

//selector导出数据给组件
export const getInfo = (state) => state.detail.info

//导出reducer
export default reducer;